<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Functions</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="static/theme.css" rel="stylesheet" type="text/css" />
<script src="static/content.js" type="text/javascript"></script>
</head>

<body>
<h1>Functions</h1>

<h2>Table of Contents</h2>
<ul>
  <li><a href="#intro">Introduction and Simple Examples</a></li>
  <li><a href="#param">Parameters</a></li>
  <li><a href="#optional">Optional Parameters</a></li>
  <li><a href="#return">Returning Values to Caller</a></li>
  <li><a href="#Variadic">Variadic Functions</a></li>
  <li><a href="#Locals">Local Variables</a></li>
  <li><a href="#DynCall">Dynamically Calling a Function</a></li>
  <li><a href="#ShortCircuit">Short-circuit Boolean Evaluation</a></li>
  <li><a href="#gosub">Using Subroutines Within a Function</a></li>
  <li><a href="#remarks">Return, Exit, and General Remarks</a></li>
  <li><a href="#include">Using #Include to Share Functions Among Multiple Scripts</a></li>
  <li><a href="#lib">Libraries of Functions: Standard Library and User Library</a></li>
  <li><a href="#BuiltIn">Built-in Functions</a></li>
</ul>
<h2><a name="intro" id="intro"></a>Introduction and Simple Examples</h2>
<p><a name="define"></a>A function is similar to a subroutine (<a href="commands/Gosub.htm">Gosub</a>) except that it can accept parameters (inputs) from its caller. In addition, a function may optionally return a value to its caller. Consider the following simple function that accepts two numbers and returns their sum:</p>
<pre>Add(x, y)
{
    return x + y   <em>; &quot;<a href="commands/Return.htm">Return</a>&quot; expects an <a href="Variables.htm#Expressions">expression</a>.</em>
}</pre>
<p>The above is known as a function <em>definition</em> because it creates a function named &quot;Add&quot; (not case sensitive) and establishes that anyone who calls it must provide exactly two parameters (x and y). To call the function, assign its result to a variable with the <strong><a href="commands/SetExpression.htm">:=</a></strong><a href="commands/SetExpression.htm"> operator</a>. For example:</p>
<pre>Var := Add(2, 3)  <em>; The number 5 will be stored in Var.</em></pre>
<p>Also, a function may be called without storing its return value:</p>
<pre>Add(2, 3)</pre>
<p>But in this case, any value returned by the function is discarded; so unless the function produces some effect other than its return value, the call would serve no purpose.</p>
<p>Since a function call is an <a href="Variables.htm#Expressions">expression</a>, any variable names in its parameter list should not be enclosed in percent signs. By contrast, literal strings should be enclosed in double quotes. For example:</p>
<pre>if <a href="#InStr">InStr</a>(MyVar, &quot;fox&quot;)
    MsgBox The variable MyVar contains the word fox.</pre>
<p>Finally, functions may be called in the parameters of any command (except OutputVar and InputVar parameters such as those of <a href="commands/StringLen.htm">StringLen</a>). However, parameters that do not support <a href="Variables.htm#Expressions">expressions</a> must use the &quot;% &quot; prefix as in this example:</p>
<pre>MsgBox <strong>%</strong> &quot;The answer is: &quot; <strong>.</strong> Add(3, 2)</pre>
<p>The &quot;% &quot; prefix is also permitted in parameters that natively support expressions, but it is simply ignored.</p>
<h2><a name="param" id="param"></a>Parameters</h2>
<p>When a function is defined, its parameters are listed in parentheses next to its name (there must be no spaces between its name and the open-parenthesis). If a function does not accept any parameters, leave the parentheses empty; for example: <code>GetCurrentTimestamp()</code>.</p>
<p><a name="ByRef"></a><strong>ByRef Parameters</strong>: From the function's point of view, parameters are essentially the same as <a href="#Locals">local variables</a> unless they are defined as <em>ByRef</em> as in this example:</p>
<pre>Swap(ByRef Left, ByRef Right)
{
    temp := Left
    Left := Right
    Right := temp
}</pre>
<p>In the example above, the use of <em>ByRef</em> causes each parameter to become an alias for the variable passed in from the caller. In other words, the parameter and the caller's variable both refer to the same contents in memory. This allows the Swap function to alter the caller's variables by moving <em>Left</em>'s contents into <em>Right</em> and vice versa.</p>
<p>By contrast, if <em>ByRef</em> were not used in the example above, <em>Left</em> and <em>Right</em> would be copies of the caller's variables and thus the Swap function would have no external effect.</p>
<p>Since <a href="commands/Return.htm">return</a> can send back only one value to a function's caller, <em>ByRef</em> can be used to send back extra results. This is achieved by having the caller pass in a variable (usually empty) in which the function stores a value.</p>
<p>When passing large strings to a function, <em>ByRef</em> enhances performance and conserves memory by avoiding the need to make a copy of the string. Similarly, using <em>ByRef</em> to send a long string back to the caller usually performs better than something like <code>Return HugeString</code>.</p>
<p><span class="ver">[AHK_L 60+]:</span> If something other than a modifiable variable is passed to a ByRef parameter, the function behaves as though the keyword "ByRef" is absent. For example, <code>Swap(A_Index, i)</code> stores the value of <i>A_Index</i> in <i>i</i>, but the value assigned to <i>Left</i> is discarded once the <i>Swap</i> function returns.</p>
<p><span class="ver">[v1.1.01+]:</span> The <a href="#IsByRef">IsByRef()</a> function can be used to determine whether the caller supplied a variable for a given ByRef parameter.</p>
<p>Known limitations:</p>
<ul>
  <li>Fields of objects are not considered variables for the purposes of <em>ByRef</em>. For example, if <code>foo.bar</code> is passed to a ByRef parameter, it will behave as though <em>ByRef</em> was omitted.</li>
  <li>It is not possible to pass <a href="misc/Clipboard.htm">Clipboard</a>, <a href="Variables.htm#BuiltIn">built-in variables</a>, or <a href="Variables.htm#env">environment variables</a> to a function's <em>ByRef</em> parameter, even when <a href="commands/_NoEnv.htm">#NoEnv</a> is absent from the script.</li>
  <li><a name="recurse"></a>Although a function may call itself recursively, if it passes one of its own <a href="#Locals">local variables</a> or non-ByRef parameters to itself <em>ByRef</em>, the new layer's <em>ByRef</em> parameter will refer to its own local variable of that name rather than the previous layer's. However, this issue does not occur when a function passes to itself a <a href="#Global">global variable</a>, <a href="#static">static variable</a>, or <em>ByRef</em> parameter.</li>
  <li>If a parameter in a function-call resolves to a variable (e.g. <code>Var</code> or <code>++Var</code> or <code>Var*=2</code>), other parameters to its left or right can alter that variable before it is passed to the function. For example, <code>func(Var, Var++)</code> would unexpectedly pass 1 and 0 when <em>Var</em> is initially 0, even when the function's first parameter is not <em>ByRef</em>. Since this behavior is counterintuitive, it might change in a future release.</li>
  <li>ByRef is not directly supported in functions called by COM clients, or when calling COM methods. Instead, the script receives or must pass a <a href="commands/ComObjActive.htm#ByRef">wrapper object</a> containing the <a href="commands/ComObjType.htm">VarType</a> and address of the value.</li>
</ul>
<h2 id="optional">Optional Parameters</h2>
<p>When defining a function, one or more of its parameters can be marked as optional. This is done by appending an equal sign (or <code>:=</code> in v1.1.09+) followed by a default value. The following function has its Z parameter marked optional:</p>
<pre>Add(X, Y, Z:=0) {
    return X + Y + Z
}</pre>
<p>As of v1.1.09, both <code>=</code> and <code>:=</code> are supported. The latter is recommended for consistency with expression assignments and compatibility with future versions of AutoHotkey.</p>
<p>When the caller passes <strong>three</strong> parameters to the function above, Z's default value is ignored. But when the caller passes only <strong>two</strong> parameters, Z automatically receives the value 0.</p>
<p id="missing">It is not possible to have optional parameters isolated in the middle of the parameter list. In other words, all parameters that lie to the right of the first optional parameter must also be marked optional. <span class="ver">[AHK_L 31+]:</span> Optional parameters may be omitted from the middle of the parameter list when calling the function, as shown below. For dynamic function calls and method calls, this requires v1.1.12+.</p>
<pre>Func(1,, 3)
Func(X, Y:=2, Z:=0) {  <em>; Note that Z must still be optional in this case.</em>
    MsgBox %X%, %Y%, %Z%
}</pre>
<p><a name="OptionalByRef"></a>In v1.0.46.13+, <a href="#ByRef">ByRef parameters</a> also support default values; for example: <code>Func(ByRef p1 = &quot;&quot;)</code>. Whenever the caller omits such a parameter, the function creates a local variable to contain the default value; in other words, the function behaves as though the keyword &quot;ByRef&quot; is absent.</p>
<p>A parameter's default value must be one of the following: <code>true</code>, <code>false</code>, a literal integer, a literal floating point number, or a quoted/literal string such as &quot;fox&quot; or &quot;&quot; (but strings in versions prior to 1.0.46.13+ support only &quot;&quot;).</p>
<h2 id="return">Returning Values to Caller</h2>
<p>As described in <a href="#intro">introduction</a>, a function may optionally <a href="commands/Return.htm">return</a> a value to its caller.</p>
<pre>
Test := returnTest()
MsgBox % Test

returnTest() {
  return 123
}
</pre>
<p>If you want to return extra results from a function, you may also use <a href="#ByRef">ByRef</a>:</p>
<pre>
returnByRef(A,B,C)
MsgBox % A "," B "," C

returnByRef(ByRef val1, ByRef val2, ByRef val3)
{
  val1 := "A"
  val2 := 100
  val3 := 1.1
  return
}
</pre>
<p><span class="ver">[v1.0.97+]:</span> <a href="Objects.htm#Usage_Objects">Objects</a> and <a href="Objects.htm#Usage_Simple_Arrays">Arrays</a> can be used to return multiple values or even named values:</p>
<pre>
Test1 := returnArray1()
MsgBox % Test1[1] "," Test1[2]

Test2 := returnArray2()
MsgBox % Test2[1] "," Test2[2]

Test3 := returnObject()
MsgBox % Test3.id "," Test3.val

returnArray1() {
  Test := [123,"ABC"]
  return Test
}

returnArray2() {
  x := 456
  y := "EFG"
  return [x, y]
}

returnObject() {
  Test := {id: 789, val: "HIJ"}
  return Test
}
</pre>
<h2 id="Variadic">Variadic Functions <span class="ver">[AHK_L 60+]</span></h2>
<p>When defining a function, write an asterisk after the final parameter to mark the function as variadic, allowing it to receive a variable number of parameters:</p>
<pre>Join(sep, <b class="blue">params*</b>) {
    for index,param in params
        str .= param . sep
    return SubStr(str, 1, -StrLen(sep))
}
MsgBox % Join("`n", "one", "two", "three")</pre>
<p>When a variadic function is called, surplus parameters can be accessed via an object which is stored in the function's final parameter. The first surplus parameter is at <code><i>params</i>[1]</code>, the second at <code><i>params</i>[2]</code> and so on. As with any standard object, <code><i>params</i>.MaxIndex()</code> can be used to determine the highest numeric index (in this case the number of parameters). However, if there are no parameters, MaxIndex returns an empty string.</p>
<p>Notes:</p>
<ul>
  <li>The "variadic" parameter can only appear at the end of the formal parameter list.</li>
  <li><a href="misc/RegExCallout.htm">RegEx callouts</a> cannot be variadic; the "variadic" parameter is tolerated but left blank.</li>
  <li><a href="commands/RegisterCallback.htm">Callbacks</a> pass surplus parameters <a href="commands/RegisterCallback.htm#Indirect">by address</a> rather than via an array.</li>
</ul>
<h3 id="VariadicCall">Variadic Function Calls</h3>
<p>While variadic functions can <i>accept</i> a variable number of parameters, an array of parameters can be passed to <i>any</i> function by applying the same syntax to a function-call:</p>
<pre>substrings := ["one", "two", "three"]
MsgBox % Join("`n", <b class="blue">substrings*</b>)</pre>
<p>Notes:</p>
<ul>
  <li>Numbering of parameters within the source array begins at 1.</li>
  <li>Optional parameters may be entirely omitted from the array.</li>
  <li>The array of parameters may contain named items when calling a user-defined function; in any other case, named items are not supported.</li>
  <li>The target function may also be variadic, in which case named items are copied even if they have no corresponding formal parameter.</li>
  <li>This syntax can also be used when calling methods or retrieving properties of objects; for example, <code>Object.Property[Params*]</code>. In v1.1.12+, it can also be used for setting properties.</li>
</ul>
<p>Known limitations:</p>
<ul>
  <li>Only the right-most parameter can be expanded this way. For example, <code>Func(x, y*)</code> is supported but <code>Func(x*, y)</code> is not.</li>
  <li>There must not be any non-whitespace characters between the asterisk (<code>*</code>) and the symbol which ends the parameter list.</li>
</ul>
<h2 id="Locals">Local and Global Variables</h2>
<h3>Local Variables</h3>
<p>All variables accessed or created inside a function are <em>local</em> by default (except <a href="#SuperGlobal">super-global</a> variables and built-in variables such as <a href="misc/Clipboard.htm">Clipboard</a>, <a href="misc/ErrorLevel.htm">ErrorLevel</a>, and <a href="Variables.htm#TimeIdle">A_TimeIdle</a>). Each local variable's contents are visible only to lines that lie inside the function. Consequently, a local variable may have the same name as a global variable and both will have separate contents. Finally, all local variables start off blank each time the function is called.</p>
<h3 id="Global">Global variables</h3>
<p>To refer to an existing global variable inside a function (or create a new one), declare the variable as global prior to using it. For example:</p>
<pre>LogToFile(TextToLog)
{
    global LogFileName  <em>; This global variable was previously given a value somewhere outside this function.</em>
    FileAppend, %TextToLog%`n, %LogFileName%
}</pre>
<p><a name="AssumeGlobal"></a><strong>Assume-global mode</strong>: If a function needs to access or create a large number of global variables, it can be defined to assume that all its variables are global (except its parameters) by making its first line either the word &quot;global&quot; or the declaration of a local variable. For example:</p>
<pre>SetDefaults()
{
    global  <em>; This word may be omitted if the first line of this function will be something like &quot;local MyVar&quot;.</em>
    MyGlobal := 33  <em>; Assigns 33 to a global variable, first creating the variable if necessary.</em>
    local x, y:=0, z  <em>; Local variables must be declared in this mode, otherwise they would be assumed global.</em>
}</pre>
<p>This assume-global mode can also be used by a function to create a global <a href="misc/Arrays.htm">array</a>, such as a loop that assigns values to <code>Array%A_Index%</code>.</p>
<p id="SuperGlobal"><strong>Super-global variables</strong> <span class="ver">[v1.1.05+]</span>: If a global declaration appears outside of any function, it takes effect for all functions by default. This avoids the need to redeclare the variable in each function. However, if a function parameter or local variable with the same name is declared, it takes precedence over the global variable. Variables created by the <a href="Objects.htm#Custom_Classes">class</a> keyword are also super-global.</p>
<h3 id="static">Static variables</h3>
<p>Static variables are always implicitly local, but differ from locals because their values are remembered between calls. For example:</p>
<pre>LogToFile(TextToLog)
{
    <strong>static</strong> LoggedLines = 0
    LoggedLines += 1  <em>; Maintain a tally locally (its value is remembered between calls).</em>
    global LogFileName
    FileAppend, %LoggedLines%: %TextToLog%`n, %LogFileName%
}</pre>
<p><a name="InitStatic"></a><strong>Static Initializers</strong>: In versions prior to 1.0.46, all static variables started off blank; so the only way to detect that one was being used for the first time was to check whether it was blank. In v1.0.46+, a static variable may be initialized to something other than <code>&quot;&quot;</code> by following it with <code>:=</code> or <code>=</code> followed by one of the following: <code>true</code>, <code>false</code>, a literal integer, a literal floating point number, or a literal/quoted string such as <code>&quot;fox&quot;</code>. For example: <code>static X:=0, Y:=&quot;fox&quot;</code>. Each static variable is initialized only once (before the script begins executing).</p>
<p><span class="ver">[AHK_L 58+]:</span> <code>Static var := expression</code> is supported. All such expressions are evaluated immediately before the script's auto-execute section in the order they are encountered in the script.</p>
<p><a name="AssumeStatic"></a><strong>Assume-static mode</strong> <span class="ver">[v1.0.48+]:</span> A function may be defined to assume that all its variables are static (except its parameters) by making its first line the word &quot;static&quot;. For example:</p>
<pre>GetFromStaticArray(WhichItemNumber)
{
    <strong>static</strong>
    static FirstCallToUs := true  <em>; A static declaration's initializer still runs only once (upon startup).</em>
    if FirstCallToUs  <em>; Create a static array during the first call, but not on subsequent calls.</em>
    {
        FirstCallToUs := false
        Loop 10
            StaticArray%A_Index% := &quot;Value #&quot; . A_Index
    }
    return StaticArray%WhichItemNumber%
}</pre>
<p>In assume-static mode, any variable that should not be static must be declared as local or global.</p>
<h3>More about locals and globals</h3>
<p>Multiple variables may be declared on the same line by separating them with commas as in these examples:</p>
<pre>global LogFileName, MaxRetries := 5
static TotalAttempts = 0, PrevResult</pre>
<p><a name="DeclareInit"></a>In v1.0.46+, a local or global variable may be initialized on the same line as its declaration by following it with <code>:=</code> or <code>=</code> followed by any <a href="Variables.htm#Expressions">expression</a> (the <code>=</code> operator behaves the same as <code>:=</code> in declarations). Unlike <a href="#InitStatic">static initializers</a>, the initializers of locals and globals execute every time the function is called, but only if/when the flow of control actually reaches them. In other words, a line like <code>local x = 0</code> has the same effect as writing two separate lines: <code>local x</code> followed by <code>x = 0</code>.</p>
<p>Because the words <em>local</em>, <em>global</em>, and <em>static</em> are processed immediately when the script launches, a variable cannot be conditionally declared by means of an <a href="commands/IfExpression.htm">IF statement</a>. In other words, a declaration inside an IF's or ELSE's <a href="commands/Block.htm">block</a> takes effect unconditionally for all lines between the declaration and the function's closing brace. Also note that it is not currently possible to declare a dynamic variable such as <code>global Array%i%</code>.</p>
<p id="PseudoArrays">For commands that create <a href="misc/Arrays.htm">pseudo-arrays</a> (such as <a href="commands/StringSplit.htm">StringSplit</a>), each variable in the resulting pseudo-array is local if the <a href="#AssumeGlobal">assume-global mode</a> is not in effect or if the pseudo-array's first element has been declared as a local variable (this is also true if one of the function's parameters is passed -- even if that parameter is <a href="#ByRef">ByRef</a> -- because parameters are similar to local variables). Conversely, if the first element has been <a href="#Global">declared global</a>, a global array is created. However, the <i>common source of confusion</i> below applies even in these cases. The first element for <a href="commands/StringSplit.htm">StringSplit</a> is ArrayName0. For other array-creating commands such as <a href="commands/WinGet.htm">WinGet List</a>, the first element is ArrayName (i.e. without the number).</p>
<p><a name="DynVar"></a><a name="Dynamic" id="Dynamic"></a>Within a function, any dynamic variable reference such as <code>Array%i%</code> always resolves to a local variable unless no variable of that name exists, in which case a global is used if it exists. If neither exists and the usage requires the variable to be created, it is created as a local variable unless the <a href="#AssumeGlobal">assume-global mode</a> is in effect. Consequently, a function can create a global <a href="misc/Arrays.htm">array</a> manually (by means such as <code>Array%i% := A_Index</code>) only if it has been defined as an <a href="#AssumeGlobal">assume-global</a> function.</p>
<p id="ArrayConfusion"><strong>Common source of confusion</strong>: Any <em>non</em>-dynamic reference to a variable creates that variable the moment the script launches. For example, when used outside a function, <code>MsgBox %Array1%</code> creates Array1 as a global the moment the script launches. Conversely, when used inside a function <code>MsgBox %Array1%</code> creates Array1 as one of the function's locals the moment the script launches (unless <a href="#AssumeGlobal">assume-global</a> is in effect), even if Array and Array0 are declared global.</p>
<h2 id="DynCall">Dynamically Calling a Function</h2>
<p>In v1.0.47.06+, a function (even a <a href="#BuiltIn">built-in function</a>) may be called dynamically via percent signs. For example, <code>%Var%(x, &quot;fox&quot;)</code> would call the function whose name is contained in <em>Var</em>. Similarly, <code>Func%A_Index%()</code> would call Func1() or Func2(), etc., depending on the current value of A_Index.</p>
<p>In v1.1.07.00+, <em>Var</em> in <code>%Var%()</code> can contain a function name, <a href="objects/Func.htm">function reference</a> or <a href="Objects.htm#Objects_as_Functions">object imitating a function</a>. If the function does not exist, the <a href="Objects.htm#Default_Base_Object">default base object</a>'s __Call meta-function is invoked instead.</p>
<p>If the function cannot be called due to one of the reasons below, the evaluation of the expression containing the call stops silently and prematurely, which may lead to inconsistent results:</p>
<ul>
  <li>Calling a nonexistent function, which can be avoided by using <code>If <a href="#IsFunc">IsFunc</a>(VarContainingFuncName)</code>. Except for <a href="#BuiltIn">built-in functions</a>, the called function's <a href="#define">definition</a> must exist explicitly in the script by means such as <a href="commands/_Include.htm">#Include</a> or a non-dynamic call to a <a href="#lib">library function</a>.</li>
  <li>Passing too few parameters, which can be avoided by checking <a href="#IsFunc">IsFunc()</a>'s return value (which is the number of mandatory parameters plus one). Note: In v1.0.48+, passing too many parameters is tolerated; each extra parameter is fully evaluated (including any calls to functions) and then discarded.</li>
</ul>
<p>Finally, a dynamic call to a function is slightly slower than a normal call because normal calls are resolved (looked up) before the script begins running.</p>
<h2 id="ShortCircuit">Short-circuit Boolean Evaluation</h2>
<p>When <em>AND, OR</em>, and the <a href="Variables.htm#ternary">ternary operator</a> are used within an <a href="Variables.htm#Expressions">expression</a>, they short-circuit to enhance performance (regardless of whether any function calls are present). Short-circuiting operates by refusing to evaluate parts of an expression that cannot possibly affect its final result. To illustrate the concept, consider this example:</p>
<pre>if (ColorName &lt;&gt; &quot;&quot; AND not FindColor(ColorName))
    MsgBox %ColorName% could not be found.</pre>
<p>In the example above, the FindColor() function never gets called if the <em>ColorName</em> variable is empty. This is because the left side of the <em>AND</em> would be <em>false</em>, and thus its right side would be incapable of making the final outcome <em>true</em>.</p>
<p>Because of this behavior, it's important to realize that any side-effects produced by a function (such as altering a global variable's contents) might never occur if that function is called on the right side of an <em>AND</em> or <em>OR</em>.</p>
<p>It should also be noted that short-circuit evaluation cascades into nested <em>AND</em>s and <em>OR</em>s. For example, in the following expression, only the leftmost comparison occurs whenever <em>ColorName</em> is blank. This is because the left side would then be enough to determine the final answer with certainty:</p>
<pre>if (ColorName = &quot;&quot; <u>OR</u> FindColor(ColorName, Region1) <u>OR</u> FindColor(ColorName, Region2))
    break   <em>; Nothing to search for, or a match was found.</em></pre>
<p>As shown by the examples above, any expensive (time-consuming) functions should generally be called on the right side of an <em>AND</em> or <em>OR</em> to enhance performance. This technique can also be used to prevent a function from being called when one of its parameters would be passed a value it considers inappropriate, such as an empty string.</p>
<p>In v1.0.46+, the <a href="Variables.htm#ternary">ternary conditional operator (?:)</a> also short-circuits by not evaluating the losing branch.</p>
<h2 id="gosub">Using Subroutines Within a Function</h2>
<p>Although a function cannot contain <a href="#define">definitions</a> of other functions, it can contain subroutines. As with other subroutines, use <a href="commands/Gosub.htm">Gosub</a> to launch them and <a href="commands/Return.htm">Return</a> to return (in which case the Return would belong to the Gosub and not the function).</p>
<p>Known limitation: Currently, the name of each subroutine (label) must be unique among those of the entire script. The program will notify you upon launch if there are duplicate labels.</p>
<p><a name="GosubPublic"></a>If a function uses <a href="commands/Gosub.htm">Gosub</a> to jump to a public subroutine (one that lies outside of the function's braces), all variables outside are global and the function's own <a href="#Locals">local variables</a> are not accessible until the subroutine returns. However, A_ThisFunc will still contain the name of the function.</p>
<p>Although <a href="commands/Goto.htm">Goto</a> cannot be used to jump from inside a function to outside, it is possible for a function to <a href="commands/Gosub.htm">Gosub</a> an external/public subroutine and then do a Goto from there.</p>
<p>Although the use of <a href="commands/Goto.htm">Goto</a> is generally discouraged, it can be used inside a function to jump to another position within the same function. This can help simplify complex functions that have many points of return, all of which need to do some clean-up prior to returning.</p>
<p>A function may contain externally-called subroutines such as <a href="commands/SetTimer.htm">timer</a>s, <a href="commands/Gui.htm#label">GUI g-labels</a>, and <a href="commands/Menu.htm">menu items</a>. This is generally done to encapsulate them in a separate file for use with <a href="commands/_Include.htm">#Include</a>, which prevents them from interfering with the script's <a href="Scripts.htm#auto">auto-execute section</a>. However, the following limitations apply:</p>
<ul>
  <li>Such subroutines should use only <a href="#static">static</a> and <a href="#Global">global</a> variables (not <a href="#Locals">locals</a>) if their function is ever called normally. This is because a subroutine <a href="misc/Threads.htm">thread</a> that interrupts a function-call thread (or vice versa) would be able to change the values of local variables seen by the interrupted thread. Furthermore, any time a function returns to its caller, all of its local variables are made blank to free their memory.</li>
  <li>Such subroutines should use only <a href="#Global">global variables</a> (not <a href="#static">static variables</a>) as <a href="commands/Gui.htm#var">GUI control variables</a>.</li>
  <li>When a function is entered by a subroutine <a href="misc/Threads.htm">thread</a>, any references to <a href="misc/Arrays.htm">dynamic variables</a> made by that thread are treated as <a href="#Global">globals</a> (including commands that create arrays).</li>
</ul>
<h2 id="remarks">Return, Exit, and General Remarks</h2>
<p>If the flow of execution within a function reaches the function's closing brace prior to encountering a <a href="commands/Return.htm">Return</a>, the function ends and returns a blank value (empty string) to its caller. A blank value is also returned whenever the function explicitly omits <a href="commands/Return.htm">Return</a>'s parameter.</p>
<p>When a function uses the <a href="commands/Exit.htm">Exit</a> command to terminate the <a href="misc/Threads.htm">current thread</a>, its caller does not receive a return value at all. For example, the statement <code>Var := Add(2, 3)</code> would leave <code>Var</code> unchanged if <code>Add()</code> exits. The same thing happens if a function causes a runtime error such as <a href="commands/Run.htm">running</a> a nonexistent file (when <a href="commands/Run.htm#UseErrorLevel">UseErrorLevel</a> is not in effect).</p>
<p>A function may alter the value of <a href="misc/ErrorLevel.htm">ErrorLevel</a> for the purpose of returning an extra value that is easy to remember.</p>
<p>To call a function with one or more blank values (empty strings), use an empty pair of quotes as in this example: <code>FindColor(ColorName, &quot;&quot;)</code>.</p>
<p>Since calling a function does not start a new <a href="misc/Threads.htm">thread</a>, any changes made by a function to settings such as <a href="commands/SendMode.htm">SendMode</a> and <a href="commands/SetTitleMatchMode.htm">SetTitleMatchMode</a> will go into effect for its caller too.</p>
<p>The caller of a function may pass a nonexistent variable or <a href="misc/Arrays.htm">array</a> element to it, which is useful when the function expects the corresponding parameter to be <a href="#ByRef">ByRef</a>. For example, calling <code>GetNextLine(BlankArray%i%)</code> would create the variable <code>BlankArray%i%</code> automatically as a <a href="#Locals">local</a> or global (depending on whether the caller is inside a function and whether it has the <a href="#AssumeGlobal">assume-global mode</a> in effect).</p>
<p>When used inside a function, <a href="commands/ListVars.htm">ListVars</a> displays a function's <a href="#Locals">local variables</a> along with their contents. This can help debug a script.</p>
<h2>Style and Naming Conventions</h2>
<p>You might find that complex functions are more readable and maintainable if their special variables are given a distinct prefix. For example, naming each parameter in a function's parameter list with a leading &quot;p&quot; or &quot;p_&quot; makes their special nature easy to discern at a glance, especially when a function has several dozen <a href="#Locals">local variables</a> competing for your attention. Similarly, the prefix &quot;r&quot; or &quot;r_&quot; could be used for <a href="#ByRef">ByRef parameters</a>, and &quot;s&quot; or &quot;s_&quot; could be used for <a href="#static">static variables</a>.</p>
<p>The <a href="commands/Block.htm#otb">One True Brace (OTB) style</a> may optionally be used to define functions. For example:</p>
<pre>Add(x, y) <strong>{</strong>
    return x + y
<strong>}</strong></pre>
<h2 id="include">Using #Include to Share Functions Among Multiple Scripts</h2>
<p>The <a href="commands/_Include.htm">#Include</a> directive may be used (<em>even at the top of a script</em>) to load functions from an external file.</p>
<p>Explanation: When the script's flow of execution encounters a function definition, it jumps over it (using an instantaneous method) and resumes execution at the line after its closing brace. Consequently, execution can never fall into a function from above, nor does the presence of one or more functions at the very top of a script affect the <a href="Scripts.htm#auto">auto-execute section</a>.</p>
<h2><a name="lib" id="lib"></a>Libraries of Functions: Standard Library and User Library <span class="ver">[v1.0.47+]</span></h2>
<p>A script may call a function in an external file without having to use <a href="commands/_Include.htm">#Include</a>. For this to work, a file of the same name as the function must exist in one of the following library directories:</p>
<pre><a href="Variables.htm#ScriptDir">%A_ScriptDir%</a>\Lib\  <em>; Local library - requires <span class="ver">AHK_L 42+</span>.</em>
<a href="Variables.htm#MyDocuments">%A_MyDocuments%</a>\AutoHotkey\Lib\  <em>; User library.</em>
path-to-the-currently-running-AutoHotkey.exe\Lib\  <em>; Standard library.</em></pre>
<p>For example, if a script calls a nonexistent function <code>MyFunc()</code>, the program searches for a file named &quot;MyFunc.ahk&quot; in the user library. If not found there, it searches for it in the standard library. If a match is still not found and the function's name contains an underscore (e.g. <code>MyPrefix_MyFunc</code>), the program searches both libraries for a file named <code>MyPrefix.ahk</code> and loads it if it exists. This allows <code>MyPrefix.ahk</code> to contain both the function <code>MyPrefix_MyFunc</code> and other related functions whose names start with <code>MyPrefix_</code>.</p>
<p><span class="ver">[AHK_L 42+]:</span> The local library is supported and is searched before the user library and standard library.</p>
<p>Only a direct function call such as <code>MyFunc()</code> can cause a library to be auto-included. If the function is only called dynamically or indirectly, such as by a timer or GUI event, the library must be explicitly included in the script.  For example: <code><a href="commands/_Include.htm">#Include</a> &lt;MyFunc&gt;</code></p>
<p>Although a library file generally contains only a single function of the same name as its filename, it may also contain private functions and subroutines that are called only by it. However, such functions should have fairly distinct names because they will still be in the global namespace; that is, they will be callable from anywhere in the script.</p>
<p>If a library file uses <a href="commands/_Include.htm">#Include</a>, the working directory for #Include is the library file's own directory. This can be used to create a redirect to a larger library file that contains that function and others related to it.</p>
<p>The <a href="Scripts.htm#ahk2exe">script compiler (ahk2exe)</a> also supports library functions. However, it requires that a copy of AutoHotkey.exe exist in the directory above the compiler directory (which is normally the case). If AutoHotkey.exe is absent, the compiler still works but library functions are not automatically included.</p>
<p>Functions included from a library perform just as well as other functions because they are pre-loaded before the script begins executing.</p>
<h2 id="BuiltIn">Built-in Functions</h2>
<p>Any optional parameters at the end of a built-in function's parameter list may be completely omitted. For example, <code>WinExist(&quot;Untitled - Notepad&quot;)</code> is valid because its other three parameters would be considered blank.</p>
<p>A built-in function is overridden if the script defines its own function of the same name. For example, a script could have its own custom WinExist() function that is called instead of the standard one. However, the script would then have no way to call the original function.</p>
<p>External functions that reside in DLL files may be called with <a href="commands/DllCall.htm">DllCall()</a>.</p>
<h3>Frequently-used Functions</h3>
<p><strong><a name="FileExist"></a>FileExist(FilePattern)</strong>: Returns a blank value (empty string) if <em>FilePattern</em> does not exist (<em>FilePattern</em> is assumed to be in <a href="Variables.htm#WorkingDir">A_WorkingDir</a> if an absolute path isn't specified). Otherwise, it returns the <a href="commands/FileGetAttrib.htm#attrib">attribute string</a> (a subset of &quot;RASHNDOCT&quot;) of the first matching file or folder. If the file has no attributes (rare), &quot;X&quot; is returned. <em>FilePattern</em> may be the exact name of a file or folder, or it may contain wildcards (* or ?). Since an empty string is seen as &quot;false&quot;, the function's return value can always be used as a quasi-boolean value. For example, the statement <code>if FileExist(&quot;C:\My File.txt&quot;)</code> would be true if the file exists and false otherwise. Similarly, the statement <code>if InStr(FileExist(&quot;C:\My Folder&quot;), &quot;D&quot;)</code> would be true only if the file exists <em>and</em> is a directory. Corresponding commands: <a href="commands/IfExist.htm">IfExist</a> and <a href="commands/FileGetAttrib.htm">FileGetAttrib</a>.</p>
<p><strong><a name="GetKeyState"></a>GetKeyState(<a href="KeyList.htm">KeyName</a> [, &quot;P&quot;</strong> or <strong>&quot;T&quot;])</strong>: Unlike the <a href="commands/GetKeyState.htm">GetKeyState command</a> -- which returns D for down and U for up -- this function returns true (1) if the key is down and false (0) if it is up. If <em><a href="KeyList.htm">KeyName</a></em> is invalid, an empty string is returned. See <a href="commands/GetKeyState.htm">GetKeyState</a> for other return values and other usage information.</p>
<p><strong><a name="InStr"></a>InStr(Haystack, Needle [, CaseSensitive = false, StartingPos = 1, Occurrence = 1])</strong>: Returns the position of an occurrence of the string <em>Needle</em> in the string <em>Haystack</em>. Unlike <a href="commands/StringGetPos.htm">StringGetPos</a>, position 1 is the first character; this is because 0 is synonymous with &quot;false&quot;, making it an intuitive &quot;not found&quot; indicator. If the parameter <em>CaseSensitive</em> is omitted or false, the search is not case sensitive (the method of insensitivity depends on <a href="commands/StringCaseSense.htm">StringCaseSense</a>); otherwise, the case must match exactly. If <em>StartingPos</em> is omitted, it defaults to 1 (the beginning of <em>Haystack</em>). Otherwise, specify 2 to start at <em>Haystack</em>'s second character, 3 to start at the third, etc. If <em>StartingPos</em> is beyond the length of <em>Haystack</em>, 0 is returned. If <em>StartingPos</em> is 0 or negative, the search is conducted in reverse (right-to-left) beginning at that offset from the end. Regardless of the value of <em>StartingPos</em>, the returned position is always relative to the first character of <em>Haystack</em>. For example, the position of &quot;abc&quot; in &quot;123abc789&quot; is always 4. Specify 2 for <i>Occurrence</i> to return the position of the second match, 3 for the third match, etc.  Related items: <a href="commands/RegExMatch.htm">RegExMatch()</a>, <a href="commands/IfInString.htm">IfInString</a>, and <a href="commands/StringGetPos.htm">StringGetPos</a>.</p>
<p><strong>RegExMatch(Haystack, NeedleRegEx [, UnquotedOutputVar = &quot;&quot;, StartingPos = 1])</strong>: Determines whether a string contains a pattern (regular expression). See <a href="commands/RegExMatch.htm">RegExMatch()</a> for details.</p>
<p><strong>RegExReplace(Haystack, NeedleRegEx [, Replacement = &quot;&quot;, OutputVarCount = &quot;&quot;, Limit = -1, StartingPos = 1])</strong>: Replaces occurrences of a pattern (regular expression) inside a string. See <a href="commands/RegExReplace.htm">RegExReplace()</a> for details.</p>
<p><strong><a name="SubStr"></a>SubStr(String, StartingPos [, Length])</strong> <span class="ver">[v1.0.46+]:</span> Copies a substring from <em>String</em> starting at <em>StartingPos</em> and proceeding rightward to include at most <em>Length</em> characters (if <em>Length</em> is omitted, it defaults to &quot;all characters&quot;). For <em>StartingPos</em>, specify 1 to start at the first character, 2 to start at the second, and so on (if <em>StartingPos</em> is beyond <em>String</em>'s length, an empty string is returned). If <em>StartingPos</em> is less than 1, it is considered to be an offset from the end of the string. For example, 0 extracts the last character and -1 extracts the two last characters (but if <em>StartingPos</em> tries to go beyond the left end of the string, the extraction starts at the first character). <em>Length</em> is the maximum number of characters to retrieve (fewer than the maximum are retrieved whenever the remaining part of the string is too short). Specify a negative <em>Length</em> to omit that many characters from the end of the returned string (an empty string is returned if all or too many characters are omitted). Related items: <a href="commands/RegExMatch.htm">RegExMatch()</a>, <a href="commands/StringMid.htm">StringMid</a>, <a href="commands/StringLeft.htm">StringLeft/Right</a>, <a href="commands/StringTrimLeft.htm">StringTrimLeft/Right</a>.</p>
<p><strong><a name="StrLen"></a>StrLen(String)</strong>: Returns the length of <em>String</em>. See <a href="commands/StringLen.htm">StrLen()</a> for details.</p>
<p><strong><a name="StrSplit"></a>StrSplit(String [, Delimiters, OmitChars])</strong> <span class="ver">[v1.1.13+]:</span> Separates a string into an array of substrings using the specified delimiters. See <a href="commands/StringSplit.htm">StrSplit()</a> for details.</p>
<p><strong><a name="WinActive"></a>WinActive([WinTitle, WinText, ExcludeTitle, ExcludeText])</strong>: Returns the <a href="commands/WinGet.htm">Unique ID (HWND)</a> of the active window if it matches the specified criteria. See <a href="commands/WinActive.htm">WinActive()</a> for details.</p>
<p><strong><a name="WinExist"></a>WinExist([WinTitle, WinText, ExcludeTitle, ExcludeText])</strong>: Returns the <a href="commands/WinGet.htm">Unique ID (HWND)</a> of the first matching window. See <a href="commands/WinExist.htm">WinExist()</a> for details.</p>
<h3>Miscellaneous Functions</h3>
<p id="Asc"><strong>Asc(String)</strong>: Returns the numeric value of the first byte or UTF-16 code unit in <em>String</em>, or 0 if <em>String</em> is empty. The return value is in the range 0 to 255 (for ANSI) or 0 to 0xFFFF (for Unicode). See <a href="Compat.htm#Format">Unicode vs ANSI</a> for details. To allow for Unicode supplementary characters, use <a href="#Ord">Ord(String)</a> instead.</p>
<p id="Chr"><strong>Chr(Number)</strong>: Returns the string (usually a single character) corresponding to the character code indicated by <em>Number</em>. The meaning of character codes greater than 127 depends on the <a href="Compat.htm#Format">string encoding</a> in use, which in turn depends on whether a <a href="Variables.htm#IsUnicode">Unicode or ANSI</a> executable is in use. If Unicode is supported, <em>Number</em> is a Unicode character code between 0 and 0x10FFFF (or 0xFFFF prior to <span class="ver">[v1.1.21]</span>); otherwise it is an ANSI character code between 0 and 255. If <em>Number</em> is not in the valid range of character codes, an empty string is returned. Common character codes include 9 (tab), 10 (linefeed), 13 (carriage return), 32 (space), 48-57 (the digits 0-9), 65-90 (uppercase A-Z), and 97-122 (lowercase a-z).</p>
<p><strong>DllCall()</strong>: Calls a function inside a DLL, such as a standard Windows API function. See <a href="commands/DllCall.htm">DllCall()</a> for details.</p>
<p><strong>FileOpen()</strong>: Provides object-oriented file I/O. See <a href="commands/FileOpen.htm">FileOpen()</a> for details.</p>
<p><strong><a name="Func"></a>Func(FunctionName)</strong> <span class="ver">[v1.1.00+]:</span> If <em>FunctionName</em> does not exist explicitly in the script (by means such as <a href="commands/_Include.htm">#Include</a> or a non-dynamic call to a <a href="#lib">library function</a>), Func() returns 0. Otherwise, it returns a <a href="Objects.htm#Function_References">reference to the function</a>. This can be used to call the function or retrieve <a href="objects/Func.htm">information</a> such as the minimum and maximum number of parameters.</p>
<p><strong><a name="GetKeyName"></a>GetKeyName(Key), GetKeyVK(Key), GetKeySC(Key)</strong> <span class="ver">[v1.1.01+]:</span> Retrieves the name/text, virtual key code or scan code of a key. <em>Key</em> can be a VK or SC code, such as "vkA2" or "sc01D", a combination of both, or a key name. For example, both <code>GetKeyName("vk1B")</code> and <code>GetKeyName("Esc")</code> return "Escape", while <code>GetKeyVK("Esc")</code> returns 27. Note that VK and SC codes must be in hexadecimal. To convert a decimal number to the appropriate format, use <code><a href="commands/Format.htm">Format</a>("vk{:x}", vk_code)</code> or <code>Format("sc{:x}", sc_code)</code>.</p>
<p><strong><a name="IsByRef"></a>IsByRef(Var)</strong> <span class="ver">[v1.1.01+]:</span> Returns 1 if <em>Var</em> is a ByRef parameter and the caller supplied a variable; or 0 if <em>Var</em> is any other kind of variable.</p>
<p><strong><a name="IsFunc"></a>IsFunc(FunctionName)</strong> <span class="ver">[v1.0.48+]:</span> If <em>FunctionName</em> does not exist explicitly in the script (by means such as <a href="commands/_Include.htm">#Include</a> or a non-dynamic call to a <a href="#lib">library function</a>), IsFunc() returns 0. Otherwise, it returns one plus the minimum number parameters (e.g. 1 for a function that requires zero parameters, 2 for a function that requires 1 parameter, etc.). For example, the statements <code>if IsFunc(&quot;MyFunc&quot;)</code> and <code>if IsFunc(VarContainingFunctionName)</code> would be true if the function exists, and false otherwise. In v1.1.00+, <i>FunctionName</i> can be a function reference instead of a name. See also:  <a href="#DynCall">Dynamic function-call</a>, <a href="Variables.htm#ThisFunc">A_ThisFunc</a></p>
<p><strong><a name="IsLabel"></a>IsLabel(LabelName)</strong>: Returns a non-zero number if <em>LabelName</em> exists in the script as a <a href="commands/Gosub.htm">subroutine</a>, <a href="Hotkeys.htm">hotkey</a>, or <a href="Hotstrings.htm">hotstring</a> (do not include the trailing colon(s) in <em>LabelName</em>). For example, the statement <code>if IsLabel(VarContainingLabelName)</code> would be true if the label exists, and false otherwise. This is useful to avoid runtime errors when specifying a dynamic label in commands such as <a href="commands/Gosub.htm">Gosub</a>, <a href="commands/Hotkey.htm">Hotkey</a>, <a href="commands/Menu.htm">Menu</a>, and <a href="commands/Gui.htm">Gui</a>. See also: <a href="misc/Labels.htm">Labels</a>.</p>
<p><strong><a name="IsObject"></a>IsObject()</strong> <span class="ver">[AHK_L 31+]:</span> Determines if a value is an object. See also: <a href="Objects.htm">Objects</a>.</p>
<p><strong>ListView and TreeView functions</strong>: See the <a href="commands/ListView.htm">ListView</a> and <a href="commands/TreeView.htm">TreeView</a> pages for details.</p>
<p><strong><a name="NumGet"></a>NumGet(VarOrAddress [, Offset = 0][, Type = &quot;UPtr&quot;])</strong>: Returns the binary number stored at the specified address+offset. See <a href="commands/NumGet.htm">NumGet</a> for details.</p>
<p><strong><a name="NumPut"></a>NumPut(Number, VarOrAddress [, Offset = 0][, Type = &quot;UPtr&quot;])</strong>: Stores a number in binary format at the specified address+offset. See <a href="commands/NumPut.htm">NumPut</a> for details.</p>
<p><strong>OnMessage(MsgNumber [, &quot;FunctionName&quot;])</strong>: Monitors a message/event. See <a href="commands/OnMessage.htm">OnMessage()</a> for details.</p>
<p id="Ord"><strong>Ord(String)</strong> <span class="ver">[v1.1.21+]</span>: Returns the ordinal value (numeric character code) of the first character in <em>String</em>. If <em>String</em> begins with a Unicode supplementary character, <em>Ord(String)</em> returns the corresponding Unicode character code (a number between 0x10000 and 0x10FFFF). Otherwise it returns the same value as <a href="#Asc">Asc(String)</a>.</p>
<p><strong>StrGet(Address [, Length] [, Encoding = None ] )</strong> <span class="ver">[AHK_L 46+]:</span> Copies a string from a memory address, optionally converting it between code pages. See <a href="commands/StrPutGet.htm">StrGet()</a> for details.</p>
<p><strong>StrPut(String, Address [, Length] [, Encoding = None ] )</strong> <span class="ver">[AHK_L 46+]:</span> Copies a string to a memory address, optionally converting it between code pages. See <a href="commands/StrPutGet.htm">StrPut()</a> for details.</p>
<p><strong>RegisterCallback()</strong>: Creates a machine-code address that when called, redirects the call to a function in the script. See <a href="commands/RegisterCallback.htm">RegisterCallback()</a> for details.</p>
<p><strong>Trim()</strong> <span class="ver">[AHK_L 31+]:</span> Trims characters from the beginning and/or end of a string. See <a href="commands/Trim.htm">Trim()</a> for details.</p>
<p><strong>VarSetCapacity(UnquotedVarName [, RequestedCapacity, FillByte])</strong>: Enlarges a variable's holding capacity or frees its memory. See <a href="commands/VarSetCapacity.htm">VarSetCapacity()</a> for details.</p>
<h3 id="Math">General Math</h3>
<p>Note: Math functions generally return a blank value (empty string) if any of the incoming parameters are non-numeric.</p>
<p><strong><a name="Abs"></a>Abs(Number)</strong>: Returns the absolute value of <em>Number</em>. The return value is the same type as <em>Number</em> (integer or floating point).</p>
<p><strong><a name="Ceil"></a>Ceil(Number)</strong>: Returns <em>Number</em> rounded up to the nearest integer (without any .00 suffix). For example, <code>Ceil(1.2)</code> is 2 and <code>Ceil(-1.2)</code> is -1.</p>
<p><strong><a name="Exp"></a>Exp(N)</strong>: Returns <em>e</em> (which is approximately 2.71828182845905) raised to the <em>N</em>th power. <em>N</em> may be negative and may contain a decimal point. To raise  numbers other than <em>e</em> to a power, use the <a href="Variables.htm#pow">** operator</a>.</p>
<p><strong><a name="Floor"></a>Floor(Number)</strong>: Returns <em>Number</em> rounded down to the nearest integer (without any .00 suffix). For example, <code>Floor(1.2)</code> is 1 and <code>Floor(-1.2)</code> is -2.</p>
<p><strong><a name="Log"></a>Log(Number)</strong>: Returns the logarithm (base 10) of <em>Number</em>. The result is formatted as <a href="commands/SetFormat.htm">floating point</a>. If <em>Number</em> is negative, an empty string is returned.</p>
<p><strong><a name="Ln"></a>Ln(Number)</strong>: Returns the natural logarithm (base e) of <em>Number</em>.  The result is formatted as <a href="commands/SetFormat.htm">floating point</a>. If <em>Number</em> is negative, an empty string is returned.</p>
<p><strong><a name="Mod"></a>Mod(Dividend, Divisor)</strong>: Modulo. Returns the remainder when <em>Dividend</em> is divided by <em>Divisor</em>. The sign of the result is always the same as the sign of the first parameter. For example, both <code>mod(5, 3)</code> and <code>mod(5, -3)</code> yield 2, but <code>mod(-5, 3)</code> and <code>mod(-5, -3)</code> yield -2. If either input is a floating point number, the result is also a floating point number. For example, <code>mod(5.0, 3)</code> is 2.0 and <code>mod(5, 3.5)</code> is 1.5. If the second parameter is zero, the function yields a blank result (empty string).</p>
<p><strong><a name="Round"></a>Round(Number [, N])</strong>: If <em>N</em> is omitted or 0, <em>Number</em> is rounded to the nearest integer. If <em>N</em> is positive number, <em>Number</em> is rounded to <em>N</em> decimal places. If <em>N</em> is negative, <em>Number</em> is rounded by <em>N</em> digits to the left of the decimal point. For example, <code>Round(345, -1)</code> is 350 and <code>Round(345, -2)</code> is 300. Unlike <a href="commands/Transform.htm">Transform Round</a>, the result has no .000 suffix whenever <em>N</em> is omitted or less than 1. In v1.0.44.01+, a value of <em>N</em> greater than zero displays exactly <em>N</em> decimal places rather than obeying <a href="commands/SetFormat.htm">SetFormat</a>. To avoid this, perform another math operation on Round()'s return value; for example: <code>Round(3.333, 1)<strong>+0</strong></code>.</p>
<p><strong><a name="Sqrt"></a>Sqrt(Number)</strong>: Returns the square root of <em>Number</em>.  The result is formatted as <a href="commands/SetFormat.htm">floating point</a>. If <em>Number</em> is negative, the function yields a blank result (empty string).</p>
<h3>Trigonometry</h3>
<p><strong><a name="Sin"></a><a name="Cos"></a><a name="Tan"></a>Sin(Number)</strong> | <strong>Cos(Number)</strong> | <strong>Tan(Number)</strong>: Returns the trigonometric sine|cosine|tangent of <em>Number</em>. <em>Number</em> must be expressed in radians.</p>
<p><strong><a name="ASin"></a>ASin(Number)</strong>: Returns the arcsine (the number whose sine is <em>Number</em>) in radians. If <em>Number</em> is less than -1 or greater than 1, the function yields a blank result (empty string).</p>
<p><strong><a name="ACos"></a>ACos(Number)</strong>: Returns the arccosine (the number whose cosine is <em>Number</em>) in radians. If <em>Number</em> is less than -1 or greater than 1, the function yields a blank result (empty string).</p>
<p><strong><a name="ATan"></a>ATan(Number)</strong>: Returns the arctangent (the number whose tangent is <em>Number</em>) in radians.</p>
<p><strong>Note:</strong> To convert a radians value to degrees, multiply it by 180/pi (approximately 57.29578). To convert a degrees value to radians, multiply it by pi/180 (approximately 0.01745329252). The value of pi (approximately 3.141592653589793) is 4 times the arctangent of 1.</p>
<h3>Other Functions</h3>
<p><a href="https://github.com/polyethene/AutoHotkey-Scripts/blob/master/Functions.ahk">Polyethene's Command Functions</a>: Provides a callable function for each AutoHotkey command that has an OutputVar. This library can be included in any script via <a href="commands/_Include.htm">#Include</a>.</p>
</body>
</html>
